#include "robotscripti.h"

#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>

TmScriptRun* g_pRobotScript = NULL;
QString strScriptDir;

QJsonValue GetScripts(QString strPath)
{
    QJsonObject jsObj;
    QJsonArray jsArray;
    QDir sourceDir(strPath);
    QFileInfoList fileInfoList = sourceDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    foreach(QFileInfo fileInfo, fileInfoList){
        if(fileInfo.isDir()){
            jsObj.insert(fileInfo.baseName(),GetScripts(fileInfo.absoluteFilePath()));
        }
        if(fileInfo.isFile()){
            jsArray.append(fileInfo.filePath());

            }
        }
    if(!jsArray.isEmpty())
    {
      return  QJsonValue(jsArray);
    }

    return QJsonValue(jsObj);
}

QJsonObject GetScriptsDirFiles()
{
    QJsonObject jsObj;
    QDir sourceDir(strScriptDir);
    QFileInfoList fileInfoList = sourceDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    foreach(QFileInfo fileInfo, fileInfoList){
        if(fileInfo.isDir()){
            QJsonObject jsTmp;
            QString strDir1 = fileInfo.baseName();
            QDir dir(fileInfo.absoluteFilePath());
            QFileInfoList fileInfoList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
            foreach(QFileInfo fileInfo, fileInfoList){
                if(fileInfo.isDir()){
                    QString strDir2 = fileInfo.baseName();
                    QDir dir(fileInfo.absoluteFilePath());
                    QJsonArray jsArray;
                    QFileInfoList fileInfoList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
                    foreach(QFileInfo fileInfo, fileInfoList){
                        if(fileInfo.isFile()){
                            QString strFilePath = fileInfo.absoluteFilePath();
                            jsArray.append(strFilePath);
                        }
                    }
                    if(!jsArray.isEmpty()){
                        jsTmp.insert(strDir2,jsArray);
                    }
                }
            }
            jsObj.insert(strDir1,jsTmp);
        }
    }
    return jsObj;
}

RobotScriptI::RobotScriptI()
{
    g_pRobotScript = new TmScriptRun();
    g_pRobotScript->start();
}

void RobotScriptI::Play(const std::string &strPath, const Ice::Current &)
{
    QString strRealPath = QString::fromUtf8(strPath.c_str(), strPath.length());
    g_pRobotScript->LoadScript(strRealPath);
}

void RobotScriptI::PlayContent(const std::string &strContent, const Ice::Current &)
{
    QByteArray bContent = QByteArray(strContent.c_str(), strContent.length());
    g_pRobotScript->LoadContent(bContent);
}

void RobotScriptI::Stop(const Ice::Current &)
{
    g_pRobotScript->SetStop();
}

bool RobotScriptI::IsPlaying(const Ice::Current &)
{
    return g_pRobotScript->GetIsRunning();
}

std::string RobotScriptI::GetPath(const Ice::Current &)
{
    QByteArray bRet = g_pRobotScript->GetScriptPath().toUtf8();
    return std::string(bRet.data(), bRet.length());
}

std::string RobotScriptI::ListScripts(const Ice::Current &)
{
    QJsonObject obj = GetScriptsDirFiles();
//    QJsonObject obj = GetScripts(strScriptDir).toObject();
    // pack
    QJsonDocument jsDoc;
    QJsonObject jsRoot;
    jsRoot.insert("type", "scripts");
    jsRoot.insert("data", obj);
    jsDoc.setObject(jsRoot);
    return jsDoc.toJson(QJsonDocument::Compact).toStdString();
}
